﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.Controls;
using YArchitech.LIB;
using YArchitech.Revit;

namespace YArchitech.Temp
{
	public partial class MainForm : System.Windows.Forms.Form
	{
		public MainForm()
		{
			this.InitializeComponent();
			new YJKProcessTools().ShowInTaskbarChange(this);
			base.Icon = Resource.YArchitechIcon;
			this.toolStripStatusLabel.Text = "";
			MenuStripHelper.Init(this, this.mainMenu, this.toolStripContainer1);
		}

		public MainForm(UIDocument uiDocument) : this()
		{
			this.RevitUIDocument = uiDocument;
			this.m_document = new HYPipeSystemDocument();
			this.hyTwinkle = new HYTwinkle();
			this.hyTwinkle.View = this.RevitUIDocument.ActiveView;
			this.m_view.HighLightElement += this.OnHighLightElement;
			this.m_view.HighLightBranch += this.OnHighLightBranch;
		}

		public UIDocument RevitUIDocument
		{
			get
			{
				return this.m_uiDocument;
			}
			set
			{
				this.m_uiDocument = value;
			}
		}

		public void Attach(HYPipeSystem pipeSystem)
		{
			this.m_document.PipeSystem = pipeSystem;
			this.m_view.Attach(this.m_document);
			this.ErrorString = "读取数据成功!";
			this.ShowMessage(this.ErrorString, this.succeedImg);
		}

		private void MainForm_Load(object sender, EventArgs e)
		{
			base.CenterToParent();
		}

		protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
		{
			if (keyData == Keys.Escape)
			{
				base.Close();
				return true;
			}
			return base.ProcessCmdKey(ref msg, keyData);
		}

		protected override void WndProc(ref Message m)
		{
			try
			{
				if (m.WParam.ToInt32() == 61472)
				{
					this.beMinimize = true;
					if (this.hyTwinkle != null)
					{
						this.hyTwinkle.Stop();
						this.ElemIds = new List<ElementId>();
						foreach (HYPipeStraightRow hypipeStraightRow in this.m_view.GetSelectRows())
						{
							ElementId elementId = new ElementId(hypipeStraightRow.AttachId);
							if (this.RevitUIDocument.Document.GetElementById(elementId) != null)
							{
								this.ElemIds.Add(elementId);
							}
						}
						base.DialogResult = DialogResult.Abort;
					}
				}
				else
				{
					base.WndProc(ref m);
				}
			}
			catch (Exception)
			{
			}
		}

		protected override void OnClosing(CancelEventArgs e)
		{
			if (this.m_document != null && this.m_document.Modified)
			{
				DialogResult dialogResult = DialogResult.No;
				if (!this.beMinimize)
				{
					dialogResult = MessageBox.Show("数据有修改，要赋回图面么？", Resources.MessageBoxTitle, MessageBoxButtons.YesNoCancel);
				}
				if (dialogResult != DialogResult.Cancel)
				{
					if (dialogResult != DialogResult.Yes)
					{
						return;
					}
					this.AssignBack();
					return;
				}
				else
				{
					e.Cancel = true;
				}
			}
		}

		protected override void OnClosed(EventArgs e)
		{
			base.OnClosed(e);
			if (this.hyTwinkle != null)
			{
				this.hyTwinkle.Dispose();
			}
		}

		public void ShowMessage(string message, Image img)
		{
			this.toolStripStatusLabel.Text = message;
			this.toolStripStatusLabel.Image = img;
		}

		private void StripItemOnClick_Open(object sender, EventArgs e)
		{
			if (this.m_document.OnOpenFile())
			{
				this.m_view.Attach(this.m_document);
				return;
			}
			this.ErrorString = this.m_document.ErrorString;
			MessageBox.Show(this.ErrorString, Resource.MessageBoxTitle, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
			this.ShowMessage(this.ErrorString, this.errorImg);
		}

		private void StripItemOnClick_Save(object sender, EventArgs e)
		{
			string directoryName = Path.GetDirectoryName(this.RevitUIDocument.Document.PathName);
			this.m_document.Path = directoryName;
			if (this.m_document.OnSaveFile())
			{
				this.ErrorString = this.m_document.ErrorString;
				this.ShowMessage(this.ErrorString, this.succeedImg);
				return;
			}
			this.ErrorString = this.m_document.ErrorString;
			MessageBox.Show(this.ErrorString, Resource.MessageBoxTitle, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
			this.ShowMessage(this.ErrorString, this.errorImg);
		}

		private void StripItemOnClick_SaveAs(object sender, EventArgs e)
		{
			string directoryName = Path.GetDirectoryName(this.RevitUIDocument.Document.PathName);
			this.m_document.Path = directoryName;
			if (this.m_document.OnSaveAs())
			{
				this.ErrorString = this.m_document.ErrorString;
				this.ShowMessage(this.ErrorString, this.succeedImg);
				return;
			}
			this.ErrorString = this.m_document.ErrorString;
			MessageBox.Show(this.ErrorString, Resource.MessageBoxTitle, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
			this.ShowMessage(this.ErrorString, this.errorImg);
		}

		private void StripItemOnClick_Exit(object sender, EventArgs e)
		{
			base.Close();
		}

		private void StripItemOnClick_Pick(object sender, EventArgs e)
		{
			base.DialogResult = DialogResult.OK;
		}

		private void StripItemOnClick_Parameters(object sender, EventArgs e)
		{
			OptionForm optionForm = new OptionForm();
			optionForm.SetOption(this.m_document.Option);
			optionForm.ShowDialog();
		}

		private void StripItemOnClick_Specification(object sender, EventArgs e)
		{
			PipeSpecificationForm pipeSpecificationForm = new PipeSpecificationForm(this.m_document.Specifications, this.m_uiDocument.Document);
			if (DialogResult.OK != pipeSpecificationForm.ShowDialog())
			{
				return;
			}
			this.m_view.InitSpecificationData();
		}

		private void StripItemOnClick_Modify(object sender, EventArgs e)
		{
			if (this.m_document == null || this.m_document.PipeSystem == null)
			{
				return;
			}
			this.m_view.UpdateData(true);
			List<HYPipeStraightRow> selectRows = this.m_view.GetSelectRows();
			if (selectRows.Count == 0)
			{
				YJKMessageBox.Show("请先选择要修改的数据行，可多选");
				return;
			}
			PipeModifyForm pipeModifyForm = new PipeModifyForm(selectRows[0].MEPObject, this.m_document);
			pipeModifyForm.Text = string.Format("共选中{0}根水管", selectRows.Count);
			if (DialogResult.OK != pipeModifyForm.ShowDialog())
			{
				return;
			}
			foreach (HYPipeStraightRow hypipeStraightRow in selectRows)
			{
				if (pipeModifyForm.ModifyFlow)
				{
					hypipeStraightRow.MEPObject.Flow = pipeModifyForm.Flow;
				}
				if (pipeModifyForm.ModifySpec)
				{
					hypipeStraightRow.MEPObject.Section = pipeModifyForm.Spec;
				}
				hypipeStraightRow.Refresh();
			}
			this.m_document.Modified = (pipeModifyForm.ModifyFlow || pipeModifyForm.ModifySpec);
		}

		private void StripItemOnClick_Design(object sender, EventArgs e)
		{
			if (this.m_document.PipeSystem == null || this.m_document.PipeSystem.IsEmpty)
			{
				YJKMessageBox.Show("请提取系统后，再进行计算！", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
				return;
			}
			this.m_view.UpdateData(true);
			if (this.hasChecked && MessageBox.Show("再次设计计算会重新计算管径，校核计算设定的管径将被复原！\n确定要设计计算么？", Resource.MessageBoxTitle, MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk) != DialogResult.Yes)
			{
				return;
			}
			if (this.m_document.OnDesign())
			{
				this.hasChecked = false;
				this.m_view.UpdateData(false);
				this.ErrorString = "设计计算成功! \n 笑脸表示计算结果符合管径规格控制数据范围\n红脸表示计算结果超出控制范围。";
				MessageBox.Show(this.ErrorString, Resource.MessageBoxTitle);
				this.ShowMessage(this.ErrorString, this.succeedImg);
				this.m_document.Modified = true;
				return;
			}
			this.ErrorString = this.m_document.ErrorString;
			MessageBox.Show(this.ErrorString, Resource.MessageBoxTitle, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
			this.ShowMessage(this.ErrorString, this.errorImg);
		}

		private void StripItemOnClick_Check(object sender, EventArgs e)
		{
			if (this.m_document.PipeSystem == null || this.m_document.PipeSystem.IsEmpty)
			{
				YJKMessageBox.Show("请提取系统后，再进行计算！", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
				return;
			}
			this.m_view.UpdateData(true);
			if (this.m_document.OnCheck())
			{
				this.hasChecked = true;
				this.m_view.UpdateData(false);
				this.ErrorString = "校核计算成功! \n 笑脸表示计算结果符合管径规格控制数据范围\n红脸表示计算结果超出控制范围。";
				MessageBox.Show(this.ErrorString, Resource.MessageBoxTitle);
				this.ShowMessage(this.ErrorString, this.succeedImg);
				this.m_document.Modified = true;
				return;
			}
			this.ErrorString = this.m_document.ErrorString;
			MessageBox.Show(this.ErrorString, Resource.MessageBoxTitle, MessageBoxButtons.OK, MessageBoxIcon.Hand);
			this.ShowMessage(this.ErrorString, this.errorImg);
		}

		private void StripItemOnClick_ViewLongest(object sender, EventArgs e)
		{
			if (this.m_document.PipeSystem == null || this.m_document.PipeSystem.IsEmpty)
			{
				YJKMessageBox.Show("请提取系统并计算后，再进行此操作！", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
				return;
			}
			this.m_view.UpdateData(true);
			string empty = string.Empty;
			if (this.m_view.ShowLongetBranch(out empty))
			{
				this.ErrorString = empty;
				this.ShowMessage(empty, this.succeedImg);
				return;
			}
			this.ErrorString = this.m_document.ErrorString;
			MessageBox.Show(this.ErrorString, Resource.MessageBoxTitle, MessageBoxButtons.OK, MessageBoxIcon.Hand);
			this.ShowMessage(this.ErrorString, this.errorImg);
		}

		private void StripItemOnClick_ViewWorst(object sender, EventArgs e)
		{
			if (this.m_document.PipeSystem == null || this.m_document.PipeSystem.IsEmpty)
			{
				YJKMessageBox.Show("请提取系统并计算后，再进行此操作！", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
				return;
			}
			this.m_view.UpdateData(true);
			string empty = string.Empty;
			if (this.m_view.ShowMaxResistanceBranch(out empty))
			{
				this.ErrorString = empty;
				this.ShowMessage(empty, this.succeedImg);
				return;
			}
			this.ErrorString = this.m_document.ErrorString;
			MessageBox.Show(this.ErrorString, Resource.MessageBoxTitle, MessageBoxButtons.OK, MessageBoxIcon.Hand);
			this.ShowMessage(this.ErrorString, this.errorImg);
		}

		private void StripItemOnClick_ViewMostUnbalanced(object sender, EventArgs e)
		{
			if (this.m_document.PipeSystem == null || this.m_document.PipeSystem.IsEmpty)
			{
				YJKMessageBox.Show("请提取系统并计算后，再进行此操作！", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
				return;
			}
			this.m_view.UpdateData(true);
			string empty = string.Empty;
			if (this.m_view.ShowMostUnbalanceBranch(out empty))
			{
				this.ErrorString = empty;
				this.ShowMessage(empty, this.succeedImg);
				return;
			}
			this.ErrorString = this.m_document.ErrorString;
			MessageBox.Show(this.ErrorString, Resource.MessageBoxTitle, MessageBoxButtons.OK, MessageBoxIcon.Hand);
			this.ShowMessage(this.ErrorString, this.errorImg);
		}

		private void StripItemOnClick_ViewAll(object sender, EventArgs e)
		{
			if (this.m_document.PipeSystem == null || this.m_document.PipeSystem.IsEmpty)
			{
				YJKMessageBox.Show("请提取系统并计算后，再进行此操作！", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
				return;
			}
			this.m_view.UpdateData(true);
			if (!this.m_view.ShowAllStraight())
			{
				this.ErrorString = this.m_document.ErrorString;
				MessageBox.Show(this.ErrorString, Resource.MessageBoxTitle, MessageBoxButtons.OK, MessageBoxIcon.Hand);
				this.ShowMessage(this.ErrorString, this.errorImg);
			}
		}

		private void StripItemOnClick_Excel(object sender, EventArgs e)
		{
			if (this.m_document.PipeSystem == null || this.m_document.PipeSystem.IsEmpty)
			{
				YJKMessageBox.Show("请提取系统并计算后，再进行此操作！", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
				return;
			}
			this.m_view.UpdateData(true);
			try
			{
				ExcelProgressBar excelProgressBar = new ExcelProgressBar(this.m_document);
				excelProgressBar.ShowDialog();
				this.ErrorString = this.m_document.ErrorString;
				if (excelProgressBar.Succeed)
				{
					this.ShowMessage(this.ErrorString, this.succeedImg);
				}
				else
				{
					this.ErrorString = this.m_document.ErrorString;
					MessageBox.Show(this.ErrorString, Resource.MessageBoxTitle, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
					this.ShowMessage(this.ErrorString, this.errorImg);
				}
			}
			catch (Exception ex)
			{
				this.ErrorString = ex.Message;
				MessageBox.Show(this.ErrorString, Resource.MessageBoxTitle, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
				this.ShowMessage(this.ErrorString, this.errorImg);
			}
		}

		private void AssignBack()
		{
			this.m_view.UpdateData(true);
			if (this.m_document == null || !this.m_document.Modified)
			{
				this.ErrorString = "还没进行计算，计算以后才能赋回图面。";
				MessageBox.Show(this.ErrorString, Resource.MessageBoxTitle, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
				this.ShowMessage(this.ErrorString, this.errorImg);
				return;
			}
			RevitPipeSystemUpdate revitPipeSystemUpdate = new RevitPipeSystemUpdate(this.m_document, this.RevitUIDocument.Document);
			if (revitPipeSystemUpdate.Update())
			{
				this.m_document.Modified = false;
				this.ErrorString = revitPipeSystemUpdate.ErrorString;
				MessageBox.Show(this.ErrorString, Resource.MessageBoxTitle, MessageBoxButtons.OK);
				this.ShowMessage(this.ErrorString, this.succeedImg);
				revitPipeSystemUpdate.UpdateTransition();
				return;
			}
			this.ErrorString = revitPipeSystemUpdate.ErrorString;
			MessageBox.Show(this.ErrorString, Resource.MessageBoxTitle, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
			this.ShowMessage(this.ErrorString, this.errorImg);
		}

		private void StripItemOnClick_AssignBack(object sender, EventArgs e)
		{
			this.AssignBack();
		}

		private void StripItemOnClick_Help(object sender, EventArgs e)
		{
			string text = HYDataAccess.hyGetHelpChm();
			string text2 = HYDataAccess.hyGetHelpIndex("水系统计算");
			if (!string.IsNullOrEmpty(text) && !string.IsNullOrEmpty(text2))
			{
				Help.ShowHelp(new System.Windows.Forms.Control(), text, text2);
			}
		}

		private void StripItemOnClick_About(object sender, EventArgs e)
		{
			new IntroSystemCal().Show();
		}

		private void StopTwinkle()
		{
			this.hyTwinkle.Stop();
		}

		private void OnHighLightElement(object sender, ElementEventArgs e)
		{
			if (this.hyTwinkle == null)
			{
				return;
			}
			this.hyTwinkle.Stop();
			ElementId elementId = new ElementId(e.ElementId);
			List<ElementId> list = new List<ElementId>();
			if (this.RevitUIDocument.Document.GetElementById(elementId) != null)
			{
				list.Add(elementId);
			}
			this.hyTwinkle.Ids = list;
			this.hyTwinkle.Start();
		}

		private void OnHighLightBranch(object sender, BranchEventArgs e)
		{
			if (this.hyTwinkle == null)
			{
				return;
			}
			this.hyTwinkle.Stop();
			HYBranch branch = e.Branch;
		}

		private HYTwinkle hyTwinkle;

		private bool hasChecked;

		private string ErrorString = "";

		private Bitmap errorImg = Resources.error16;

		private Bitmap succeedImg = Resources.successface16;

		private Bitmap waitingImg = Resources.waitingface16;

		private HYPipeSystemDocument m_document;

		private UIDocument m_uiDocument;

		private const int SC_MINIMIZE = 61472;

		private bool beMinimize;

		public List<ElementId> ElemIds;
	}
}
